package cn.maihe.elg.operation.centers.fuzhou.auth;

import cn.maihe.elg.operation.utils.ValidateUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @Description
 * @Author WJH
 * @Date 2022/03/31
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FZHeaderInfo {
    /**
     * String	接口版本号，默认 WYS01
     */
//    @NotBlank
    private String version;

    /**
     * String	平台将为每个接入方系统提供一个appId
     */
    @NotBlank
    private String appid;
    /**
     * String	采用32位MD5算法（大写）计算获得，sign=MD5(appId + requestId + slat + timeStamp + BASE64_ENC(httpbody))
     * slat由保函平台分配
     */
    @NotBlank
    private String sign;
    /**
     * String	交易流水，每笔请求唯一标识
     */
    @NotBlank
    private String requestid;
    /**
     * String	时间戳
     */
    @NotBlank
    private String timestamp;

    public static FZHeaderInfo of(Map<String, Object> headerMap) {
        FZHeaderInfo headerInfo = new FZHeaderInfo();
        headerInfo.setVersion((String) headerMap.get("version"));
        headerInfo.setAppid((String) headerMap.get("appid"));
        headerInfo.setSign((String) headerMap.get("sign"));
        headerInfo.setRequestid((String) headerMap.get("requestid"));
        headerInfo.setTimestamp((String) headerMap.get("timestamp"));
        try {
            ValidateUtil.validate(headerInfo);
            return headerInfo;
        } catch (Exception e) {
            throw new RuntimeException("请求头参数错误:" + e.getMessage(), e);
        }
    }

    public static FZHeaderInfo of(String requestId, String appId, String timestamp, String sign, String version) {
        FZHeaderInfo headerInfo = new FZHeaderInfo();
        headerInfo.setVersion(version);
        headerInfo.setAppid(appId);
        headerInfo.setSign(sign);
        headerInfo.setRequestid(requestId);
        headerInfo.setTimestamp(timestamp);
        try {
            ValidateUtil.validate(headerInfo);
            return headerInfo;
        } catch (Exception e) {
            throw new RuntimeException("请求头参数错误:" + e.getMessage(), e);
        }
    }

}
